//
//  XCUserAccount.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/2/28.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

class XCUserAccount: NSObject,NSCoding {
    
    
    var uid :String? //当前授权用户的UID。
    var expiresDate : NSDate?
     //access_token的生命周期，单位是秒数。
    var expires_in : NSTimeInterval = 0{
        didSet{
        expiresDate = NSDate.init(timeIntervalSinceNow: expires_in)
        }
    }
    
    var access_token :String? //用于调用access_token，接口获取授权后的access token。
    
///  用户图标
    var avatar_large :String?
    /// 用户昵称
    var screen_name : String?
    
// 字典转模型
    init(dict : [String : AnyObject]) {
        super.init()
        // 只有 super.init() 后,对象才创建完毕,对象创建完毕后才能调用对象的方法
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    
// MARK:- 归档与解档
    func encodeWithCoder(aCoder: NSCoder) {
           aCoder.encodeObject(uid, forKey: "uid")
           aCoder.encodeDouble(expires_in, forKey: "expires_in")
           aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")

    }
    required init?(coder aDecoder: NSCoder) {
       access_token =  aDecoder.decodeObjectForKey("access_token") as? String
       uid =  aDecoder.decodeObjectForKey("uid") as? String
       expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    
    
}
